package com.cacho.s2b.lesson.learn;

import org.apache.commons.lang3.StringUtils;
import org.ini4j.Ini;
import org.ini4j.Profile;

import java.io.InputStream;

/**
 * @Description org.ini4j 读取配置文件ini
 * @Author LingWei
 * @date 2023/12/17
 **/
public class LearnIni4j {
    private String iniFile;  // config.ini位置
    public LearnIni4j(){
        this("/data/config.ini");} // 如果不带参数，传参到带参数的构造函数
    public LearnIni4j(String iniFile){
        this.iniFile =iniFile;
        System.out.println("带参数调用");
        initApiTesting(iniFile);  //带参数
    };
    public static void main (String[] args){
        LearnIni4j ini = new LearnIni4j();
    }
    private void initApiTesting(String initFile){
        Ini ini = new Ini();
        InputStream in = getClass().getResourceAsStream(initFile);
        try {
           ini.load(in);
            System.out.println(ini);
            String runEnv = ini.get("run","run.env"); // section,key
            String overrideEnv = System.getenv("ENV");  // 得到ENV的环境变量
            runEnv = StringUtils.isEmpty(overrideEnv) ? runEnv : overrideEnv;
            System.out.println("运行环境为："+runEnv);
            Profile.Section section = ini.get(runEnv.toLowerCase()); //
            System.out.println("运行环境介绍："+section.get("desc")); // section下面的key
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
